package map_;

import java.util.*;

/**
 * @author DCSGO
 * @version 1.0
 * 使用HashMap添加3个员工对象
 * 要求：
 * key: 员工id
 * value: 员工对象
 * 并遍历显示工资 > 18000的员工（遍历方式最少两种）
 * 员工类：姓名、工资、员工id
 */

@SuppressWarnings({"all"})
public class MapExercise {
    public static void main(String[] args) {
        HashMap hashMap = new HashMap();
        Employee emp1 = new Employee("小王", 1212, 11999);
        Employee emp2 = new Employee("老张", 1233, 18999);
        Employee emp3 = new Employee("老刘", 3112, 21999);
        hashMap.put(emp1.getId(), emp1);
        hashMap.put(emp2.getId(), emp2);
        hashMap.put(emp3.getId(), emp3);

        //1.keySet
        Set keySet = hashMap.keySet();
        for (Object key : keySet) {
            Employee emp = (Employee) hashMap.get(key);
            if (emp.getSalary() > 18000) {
                System.out.println(emp);
            }
        }
        //Employee{name='老张', id=1233, salary=18999}
        //
        //Employee{name='老刘', id=3112, salary=21999}

        //2.EntrySet
        Set entrySet = hashMap.entrySet();
        Iterator EntIte = entrySet.iterator();
        while (EntIte.hasNext()) {
            Employee emp = (Employee) (((Map.Entry) EntIte.next()).getValue());
            if (emp.getSalary() > 18000) {
                System.out.println(emp);
            }
        }
        //Employee{name='老张', id=1233, salary=18999}
        //
        //Employee{name='老刘', id=3112, salary=21999}
    }
}

class Employee {
    private String name;
    private int id;
    private int salary;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public Employee(String name, int id, int salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return id == employee.id && salary == employee.salary && Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, id, salary);
    }

    @Override
    public String toString() {
        return "\nEmployee{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", salary=" + salary +
                '}';
    }
}
